package com.tzinfo.tzt.activity;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.util.GPSThread;
import com.tzinfo.tzt.activity.util.MyApplication;
import com.tzinfo.tzt.service.HouseCallService;
import com.tzinfo.tzt.service.HouseCollectionService;
import com.tzinfo.tzt.service.HouseHistoryService;
import com.tzinfo.tzt.service.HouseTuanService;
import com.tzinfo.tzt.util.SharedPreferencesTools;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.update.UmengUpdateAgent;

@SuppressWarnings("deprecation")
public class BaseActivity extends Activity {

	protected SharedPreferencesTools sharedPreferences;
	public boolean isExit = false;
	MyApplication mApp;
	protected ProgressBar mProgressBar;
	protected HouseHistoryService historyService;
	protected HouseCollectionService collectionService;
	protected HouseCallService callService;
	protected HouseTuanService tuanService;
	// 友盟相关
	protected final UMSocialService mController = UMServiceFactory
			.getUMSocialService("com.umeng.share", RequestType.SOCIAL);

	@SuppressWarnings("rawtypes")
	public void LoadActivity(Class ClassName, boolean needFinish) {
		startActivity(new Intent(BaseActivity.this, ClassName));
		overridePendingTransition(R.anim.left_in, R.anim.left_out);
		if (needFinish) {
			finish();
		}
	}

	protected void removeProgressBar() {
		mProgressBar.setVisibility(View.GONE);
	}

	protected void showProgressBar() {
		mProgressBar.setVisibility(View.VISIBLE);

	}

	@SuppressWarnings("rawtypes")
	public void LoadActivityWithTabhost(Intent intent, Class ClassName) {
		LinearLayout container = (LinearLayout) ((ActivityGroup) getParent())
				.getWindow().findViewById(R.id.container);
		container.removeAllViews();
		intent.setClass(BaseActivity.this, ClassName);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Window subActivity = ((ActivityGroup) BaseActivity.this.getParent())
				.getLocalActivityManager().startActivity(ClassName.toString(),
						intent);
		container.addView(subActivity.getDecorView());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mApp = (MyApplication) getApplication();
		sharedPreferences = new SharedPreferencesTools(this);
		mApp.setExit(false);
		historyService = new HouseHistoryService(this);
		collectionService = new HouseCollectionService(this);
		callService = new HouseCallService(this);
		tuanService = new HouseTuanService(this);
		// 获取GPS
		if (MyApplication.location == null) {
			// MyApplication.location = new GPSTools(this).getLocation();
			GPSThread.getInstance(this).requestLocation();
		}

		// 设置分享平台
		mController.getConfig().setPlatformOrder(SHARE_MEDIA.SINA,
				SHARE_MEDIA.SMS, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE);
		mController.getConfig().removePlatform(SHARE_MEDIA.TENCENT,
				SHARE_MEDIA.EMAIL, SHARE_MEDIA.QZONE, SHARE_MEDIA.RENREN,
				SHARE_MEDIA.DOUBAN);

		// 友盟更新
		UmengUpdateAgent.update(this);

		Log.e("my", "create");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.e("my", "start");
		if (mApp.isExit()) {
			finish();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		System.gc();
		isExit = false;
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		isExit = true;
	}

	public void FinishActivity() {
		finish();
		overridePendingTransition(R.anim.right_in, R.anim.right_out);
	}

	public void backbtn(View view) {
		FinishActivity();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			FinishActivity();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	public MyApplication GetApplication() {
		return (MyApplication) this.getApplicationContext();
	}

	@Override
	public Object getSystemService(String name) {
		// TODO Auto-generated method stub
		return super.getSystemService(name);
	}

}